package com.mlog.njyjy.ndop.monitor.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mlog.njyjy.ndop.monitor.dao.entity.TMonitorHost;
import com.mlog.njyjy.ndop.monitor.dao.entity.TMonitorHostGroup;
import com.mlog.njyjy.ndop.monitor.pojo.ResponseMessage;
import com.mlog.njyjy.ndop.monitor.service.ZabbixMonitorService;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/api")
@Api(value = "api", description = "服务器监控接口")
public class MonitorController {

	@Autowired
	ZabbixMonitorService service;

	@GetMapping(value = "/hostgroup")
	@ApiOperation(value = "获取主机组", httpMethod = "GET")
	public ResponseMessage listHostGroups() {
		return ResponseMessage.success(service.listHostGroups());
	}

	@GetMapping(value = "/host")
	@ApiOperation(value = "获取主机", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "hostid", dataType = "String", required = false, value = "2选1，主机id，返回主机监控信息"),
			@ApiImplicitParam(paramType = "query", name = "groupid", dataType = "String", required = false, value = "2选1，主机组id，返回组内所有主机") })
	public ResponseMessage getHost(String hostid, String groupid) {
		if (hostid != null)
			return ResponseMessage.success(service.getHostInfo(hostid));
		if (groupid != null)
			return ResponseMessage.success(service.listHostsByGroup(groupid));
		return ResponseMessage.success(service.listHosts());
	}

	@GetMapping(value = "/item")
	@ApiOperation(value = "获取主机所有监控项", httpMethod = "GET")
	@ApiImplicitParam(paramType = "query", name = "hostid", dataType = "String", required = true, value = "主机id")
	public ResponseMessage getItems(String hostid) {
		return ResponseMessage.success(service.getHostLiveData(hostid));
	}

	@PostMapping(value = "/host")
	@ApiOperation(value = "配置主机", httpMethod = "POST")
	@ApiImplicitParam(paramType = "body", name = "hosts", required = true, value = "参考/host结构")
	public ResponseMessage setHosts(@RequestBody List<TMonitorHost> hosts) {
		service.setHost(hosts);
		return ResponseMessage.success();
	}

	@PostMapping(value = "/hostgroup")
	@ApiOperation(value = "配置主机组", httpMethod = "POST")
	@ApiImplicitParam(paramType = "body", name = "groups", required = true, value = "参考/hostgroup结构")
	public ResponseMessage setHostGroups(@RequestBody List<TMonitorHostGroup> groups) {
		service.setHostGroup(groups);
		return ResponseMessage.success();
	}

	@GetMapping(value = "/event/view")
	@ApiOperation(value = "告警一览", httpMethod = "GET")
	@ApiImplicitParam(paramType = "query", name = "groupid", dataType = "String", required = false, value = "yyyyMMddHHmmss")
	public ResponseMessage event(String groupid) {
		return ResponseMessage.success(service.getEventView(groupid));
	}

	@GetMapping(value = "/event/table")
	@ApiOperation(value = "告警表格", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "beginTime", dataType = "String", required = true, value = "yyyyMMddHHmmss"),
			@ApiImplicitParam(paramType = "query", name = "endTime", dataType = "String", required = true, value = "yyyyMMddHHmmss"),
			@ApiImplicitParam(paramType = "query", name = "keyWord", dataType = "String", required = false, value = "可选过滤，关键词"),
			@ApiImplicitParam(paramType = "query", name = "groupids", dataType = "String", required = false, value = "可选过滤，主机组id多个用,"),
			@ApiImplicitParam(paramType = "query", name = "levels", dataType = "String", required = false, value = "可选过滤，0未分组/1信息/2警告/3一般严重/4严重/5灾难"),
			@ApiImplicitParam(paramType = "query", name = "ack", dataType = "String", required = false, value = "可选过滤，1：已确认 0：未确认") })
	public ResponseMessage event(String beginTime, String endTime, String keyWord, String groupids, String level, String ack) {
		return ResponseMessage.success(
				service.getEventList(DateUtil.parse(beginTime), DateUtil.parse(endTime), keyWord, groupids, level, ack));
	}
	
	@GetMapping(value = "/event/list")
	@ApiOperation(value = "获取主机告警列表", httpMethod = "GET")
	@ApiImplicitParam(paramType = "query", name = "hostid", dataType = "String", required = true, value = "主机id")
	public ResponseMessage getEventListByHost(String hostid) {
		return ResponseMessage.success(service.getEventListByHost(hostid));
	}

	@GetMapping(value = "/diagram/{type}")
	@ApiOperation(value = "获取主机时序图", httpMethod = "GET")
	public ResponseMessage getEventListByHost(@PathVariable("type")String type, String hostid) {
		return ResponseMessage.success(service.getTimeSeqData(type, hostid));
	}
	
	@GetMapping(value = "/focus")
	@ApiOperation(value = "获取特别关注主机状态", httpMethod = "GET")
	public ResponseMessage getFocusHosts(String groupid) {
		return ResponseMessage.success(service.getFocusHosts(groupid));
	}
}
